#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on 2011/10/7
@author: sunny lin
'''
__author__ = "Sunny Lin"
__version__ = "0.13"

import initEnv 
import time
#from unittest import * 
from com.android.monkeyrunner import MonkeyDevice,MonkeyRunner 
from aaf.monkeyCircus import MonkeyTrick
from aaf.monkeyErrors import *

class CalculationTrickBase(MonkeyTrick):
  ''' Provide as a base class, so we can reuse the __init__, setUp and tearDown and need not recode in every Calculator Test Case.'''
    
  def setUp(self, reporter=None):
    ''' Setup is an startup point, when ever start test, means to set up any need before test. 
    Here we run and startup the calculator program.
    '''
    pass
    MonkeyTrick.setUp(self)
    self.my_device.startApp('com.android.calculator2/.Calculator')
    #self.my_device.testPic()
    
  def tearDown(self, reporter=None):
    ''' tearDown is an exist point, when ever test en.d, means to destroy, or set back to origin after test. 
    Here we press Home key to exit calculator program
    '''
    MonkeyTrick.tearDown(self)
    self.my_device.home()
  
  def calculate(self, expression):
    ''' Define a common and readable method to simply usage '''
    for c in expression :
      time.sleep(0.2) # make a short delay, the time span just depend on how you feel
      self.my_device.touchArea(c)
      
  def clear(self):
    ''' Another example to define a more readable method '''
    self.my_device.longTouchArea("clear")
# End CalculationTestBase 

class CalculationTrick(CalculationTrickBase):
  def preStep(self):
    ''' Clear result before calcuate '''
    self.clear()
    
  ''' An example test case for test calculator function '''
  def testStep1(self, reporter):
    ''' Test plus function : 23.4+43.2'''
    reporter.logger.debug("test .............")
    #err=MonkeyFailure(No, ("%s failed. Snapshot is saved:%s" % (stepname, pic_file+".png")))
    #reporter.recorder.recordException(self, 0, "testStep1", err) 
    self.calculate("23.4+43.2@")

  def testStep2(self, reporter):
    ''' Test minus function : 23.4-43.2'''
    self.calculate("23.4-43.2@")
    
  def testStep3(self, reporter):
    ''' Test multiple function : 23.4*43.2'''
    self.calculate("23.4*43.2@")

  def testStep4(self, reporter):
    ''' Test divide function : 23.4/43.2'''
    self.calculate("23.4/43.2@")

class CalculationPlusTrick(CalculationTrickBase):
  ''' An example test case for test calculator plus function '''
  def testStep1(self, reporter):
    " Test 300+53="
    #self.device.type("300+53=")
    self.calculate("300.5+50.3@")
    
  def testStep2(self, reporter):
    " Test 234.09+41245.8="
    self.calculate("234.09+41245.8@")
       
# End CalculationPlusTrick

class CalculationMultipleTrick(CalculationTrickBase):
  ''' An example test case for test calculator multiple function  and use original MonkeyRunner API'''
  def testStep1(self, reporter):
    " Test 33*53  "
    self.calculate("33*53@")
    
  def testStep2(self, reporter):
    " Test 3*5  "
    self.calculate("3*5@")
# End CalculationMultipleTrick

from aaf.monkeyCircus import MonkeyShow, MonkeyTamer

def runTest():
  testshow=MonkeyShow([CalculationPlusTrick, CalculationMultipleTrick])
  MonkeyTamer().run(testshow)

if __name__ == '__main__':
  runTest()
  